<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2022/4/7.
 */

namespace Zane\TableHandle;

use App\Exceptions\ServiceException;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class TableHandle
{
    public static function make($type): IBase
    {
        $type = Str::studly($type);
        $class = "Zane\\TableHandle\\Components\\{$type}";

        try {
            return new $class();
        } catch (\Exception $exception) {
            throw new ServiceException(['message' => '【'.$type.'】数据类型未定义']);
        }
    }

    // 验证字段在表中是否存在
    public static function existsFieldName($tableName, $fieldName): bool
    {
        return Schema::hasColumn($tableName, $fieldName);
    }
}
